package com.learner.course.service.impl;

import com.learner.common.util.JwtInfo;
import com.learner.course.repository.CourseCommentRepository;
import com.learner.course.service.CourseBaseService;
import com.learner.course.service.CourseCommentService;
import com.learner.pojo.course.CourseBase;
import com.learner.pojo.course.CourseComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 评论逻辑处理类
 * @Author: ice2020x
 * @Date: 2021/11/12
 */
@Service
public class CourseCommentServiceImpl implements CourseCommentService {

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private CourseCommentRepository commentRepository;


    @Override
    public CourseComment save(String courseId, JwtInfo memberIdByJwtToken, String context) {
            CourseBase courseBase = courseBaseService.getCourseBaseById(courseId);
            CourseComment courseComment = new CourseComment();
            courseComment.setCourseId(courseId);
            courseComment.setMemberId(memberIdByJwtToken.getUserId());
            courseComment.setAvatar(memberIdByJwtToken.getAvatar());
            courseComment.setTeacherId(courseBase.getUserId());
            courseComment.setNickname(memberIdByJwtToken.getNickname());
            courseComment.setContent(context);
            return  commentRepository.save(courseComment);
    }

    @Override
    public void delete(String commentId) {
        commentRepository.deleteById(commentId);
    }

    @Override
    public List<CourseComment> findCommentListById(String courseId) {
        return commentRepository.findByCourseIdOrderByGmtCreateAsc(courseId);
    }
}
